package com.teprinciple.wanandroid.ui.page

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import com.teprinciple.wanandroid.R
import com.teprinciple.wanandroid.data.Article
import com.teprinciple.wanandroid.ui.theme.black1
import com.teprinciple.wanandroid.ui.theme.black2
import com.teprinciple.wanandroid.ui.theme.blue1
import com.teprinciple.wanandroid.ui.theme.gray1
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.util.lerp
import com.google.accompanist.glide.GlideImage
import com.google.accompanist.pager.*
import com.teprinciple.wanandroid.data.BannerData
import kotlin.math.absoluteValue

@ExperimentalPagerApi
@Composable
fun HomePage() {

    Log.e("this", "------------HomePage-----------------")

    Column(Modifier.background(Color.White)) {
        LazyColumn(content = {
            //  banner
            item {
                Box() {
                    val banner = BannerData.testData
                    val pagerState = rememberPagerState(pageCount = banner.size)
                    HorizontalPager(
                        state = pagerState,
                        modifier = Modifier.fillMaxWidth().height(180.dp)
                    ) { page ->

                        GlideImage(
                            data = banner[page].imagePath,
                            contentDescription = null,
                            contentScale = ContentScale.Crop,
                            modifier = Modifier.height(180.dp).fillMaxWidth()
                        )
                    }
                    HorizontalPagerIndicator(
                        pagerState = pagerState,
                        modifier = Modifier
                            .align(Alignment.BottomCenter)
                            .padding(8.dp)
                    )
                }
            }

            val list = ArrayList<Article>()
            for (i in 0..100) {
                list.addAll(Article.testData)
            }

            items(list) { item ->
                HomeArticleItem(article = item)
            }
        })
    }
}

@Composable
fun HomeArticleItem(article: Article) {

    var collect by remember {
        mutableStateOf(article.collect)
    }

    Column(Modifier.padding(14.dp)) {
        Row(Modifier.fillMaxWidth()) {

            if (article.fresh) {
                Image(
                    painter = painterResource(id = R.mipmap.ic_new),
                    contentDescription = "new",
                    modifier = Modifier.size(18.dp)
                )
            }

            Text(
                text = article.shareUser,
                fontSize = 11.sp,
                color = blue1
            )

            // TODO tag
            Box(Modifier.weight(1f)) {

            }

            Text(
                text = article.niceShareDate,
                fontSize = 11.sp,
                color = gray1
            )
        }

        Row(Modifier.padding(top = 4.dp)) {

            if (article.envelopePic.isNotEmpty()) {
                GlideImage(
                    data = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi3.sinaimg.cn%2FIT%2F2009%2F1122%2F20091122131121.jpg&refer=http%3A%2F%2Fi3.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1619075588&t=c3ba48a202160bfc8a46f70daf24253b",
                    contentDescription = "封面",
                    contentScale = ContentScale.FillBounds,
                    modifier = Modifier.width(120.dp).height(90.dp).padding(end = 10.dp).clip(
                        RoundedCornerShape(4.dp)
                    )
                )
            }

            Column() {
                Text(
                    text = article.title,
                    maxLines = if (article.desc.isNotEmpty()) 1 else 2,
                    overflow = TextOverflow.Ellipsis,
                    fontSize = 16.sp,
                    color = black1,
                    lineHeight = 1.2f.em
                )

                if (article.desc.isNotEmpty()) {
                    Text(
                        text = article.desc,
                        maxLines = 3,
                        overflow = TextOverflow.Ellipsis,
                        fontSize = 14.sp,
                        color = black2,
                        lineHeight = 1.4f.em,
                        modifier = Modifier.padding(top = 5.dp)
                    )
                }
            }
        }

        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.fillMaxWidth().padding(top = 4.dp)
        ) {
            Text(
                text = "${article.superChapterName}·${article.chapterName}",
                fontSize = 11.sp,
                color = gray1
            )
            Image(
                painter = painterResource(
                    id =
                    if (collect) R.mipmap.ic_collect_fill else R.mipmap.ic_collect_strock
                ),
                contentDescription = "收藏",
                modifier = Modifier.size(18.dp).clickable {
                    collect = !collect
                    article.collect = collect
                }
            )
        }
    }
}